<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全景图查看工具</title>
    <!-- 引入Three.js库 -->
    <script src="../js/three.min.js"></script>
    <script src="../js/OrbitControls.js"></script>
    <!-- 引入Font Awesome图标库 -->
    <link rel="stylesheet" href="../css/all.min.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        #container {
            position: absolute;
            width: 100%;
            height: 100%;
        }
        
        #loading {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 20px;
            border-radius: 5px;
            display: none;
            z-index: 100;
        }
        
        #toolbar {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(0, 0, 0, 0.5);
            padding: 10px;
            border-radius: 25px;
            display: flex;
            gap: 5px;
            backdrop-filter: blur(5px);
            z-index: 10;
        }
        
        .tool-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;
            transition: all 0.3s ease;
        }
        
        .tool-btn:hover {
            background: rgba(255, 255, 255, 0.3);
            transform: scale(1.1);
        }
        
        .tool-btn .tooltip {
            position: absolute;
            bottom: 50px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            white-space: nowrap;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s;
        }
        
        .tool-btn:hover .tooltip {
            opacity: 1;
        }
        
        #upload-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.9);
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            color: white;
            z-index: 20;
            transition: all 0.3s ease;
        }
        
        #upload-container.hidden {
            opacity: 0;
            pointer-events: none;
        }
        
        #file-input {
            display: none;
        }
        
        .upload-btn {
            background: #4285F4;
            padding: 15px 30px;
            font-size: 16px;
            margin: 20px 0;
            border-radius: 30px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s ease;
        }
        
        .upload-btn:hover {
            background: #3367D6;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        }
        
        #progress-container {
            position: absolute;
            bottom: 80px;
            left: 50%;
            transform: translateX(-50%);
            width: 80%;
            max-width: 500px;
            background: rgba(0, 0, 0, 0.5);
            padding: 10px;
            border-radius: 10px;
            display: none;
            z-index: 10;
            backdrop-filter: blur(5px);
        }
        
        #progress-bar {
            width: 100%;
            height: 10px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 5px;
            overflow: hidden;
            margin-top: 5px;
        }
        
        #progress {
            height: 100%;
            background: #4285F4;
            width: 0%;
            transition: width 0.3s ease;
        }
        
        .modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 100;
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s ease;
        }
        
        .modal.active {
            opacity: 1;
            pointer-events: all;
        }
        
        .modal-content {
            background: rgba(0, 0, 0, 0.8);
            padding: 20px;
            border-radius: 10px;
            width: 300px;
            max-width: 90%;
            backdrop-filter: blur(5px);
            color: white;
        }
        
        .modal-title {
            margin-top: 0;
            margin-bottom: 15px;
        }
        
        .modal-buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }
        
        .modal-btn {
            padding: 8px 15px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
        }
        
        .modal-btn-primary {
            background: #4285F4;
            color: white;
        }
        
        @media (max-width: 768px) {
            #toolbar {
                bottom: 10px;
                padding: 8px;
            }
            
            .tool-btn {
                width: 36px;
                height: 36px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div id="container"></div>
    
    <div id="loading">
        <i class="fas fa-spinner fa-spin"></i> 加载中，请稍候...
    </div>
    
    <div id="toolbar">
        <button class="tool-btn" id="fullscreen-btn" title="全屏">
            <i class="fas fa-expand"></i>
            <span class="tooltip">全屏</span>
        </button>
        <button class="tool-btn" id="upload-btn" title="选择全景图">
            <i class="fas fa-file-image"></i>
            <span class="tooltip">选择全景图</span>
        </button>
        <button class="tool-btn" id="reset-view-btn" title="重置视角">
            <i class="fas fa-crosshairs"></i>
            <span class="tooltip">重置视角</span>
        </button>
    </div>
    
    <div id="progress-container">
        <div id="progress-text">选择进度: 0%</div>
        <div id="progress-bar">
            <div id="progress"></div>
        </div>
    </div>
    
    <div id="upload-container">
        <h2>全景图查看工具</h2>
        <p>选择一张全景图(360°x180°)，支持JPG/PNG格式</p>
        <label for="file-input" class="upload-btn">
            <i class="fas fa-file-image"></i> 选择全景图文件
        </label>
        <input type="file" id="file-input" accept="image/jpeg,image/png">
    </div>
    
    <!-- 提示消息模态框 -->
    <div id="message-modal" class="modal">
        <div class="modal-content">
            <h3 class="modal-title" id="message-title">提示</h3>
            <p id="message-content">操作成功</p>
            <div class="modal-buttons">
                <button id="close-message" class="modal-btn modal-btn-primary">确定</button>
            </div>
        </div>
    </div>
    
    <script>
        // 全局变量
        let scene, camera, renderer, controls, sphere, texture;
        
        // 初始化Three.js场景
        function init() {
            // 创建场景
            scene = new THREE.Scene();
            
            // 创建相机
            camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            camera.position.z = 0.01;
            
            // 创建渲染器
            renderer = new THREE.WebGLRenderer({ antialias: true });
            renderer.setPixelRatio(window.devicePixelRatio);
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.getElementById('container').appendChild(renderer.domElement);
            
            // 添加事件监听器
            setupEventListeners();
            
            // 开始动画循环
            animate();
        }
        
        // 创建全景球体
        function createPanorama(texture) {
            // 如果已有球体，先移除
            if (sphere) {
                scene.remove(sphere);
                sphere.geometry.dispose();
                sphere.material.dispose();
            }
            
            // 创建球体几何体
            const geometry = new THREE.SphereGeometry(500, 60, 40);
            geometry.scale(-1, 1, 1);
            
            // 创建材质
            const material = new THREE.MeshBasicMaterial({
                map: texture
            });
            
            // 创建球体网格
            sphere = new THREE.Mesh(geometry, material);
            scene.add(sphere);
            
            // 创建轨道控制器
            if (controls) {
                controls.dispose();
            }
            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.enableZoom = true;
            controls.enablePan = false;
            controls.enableDamping = true;
            controls.dampingFactor = 0.05;
            controls.rotateSpeed = 0.5;
            controls.minDistance = 0.1;
            controls.maxDistance = 3;
        }
        
        // 设置事件监听器
        function setupEventListeners() {
            // 窗口大小调整
            window.addEventListener('resize', onWindowResize);
            
            // 文件选择
            document.getElementById('file-input').addEventListener('change', handleFileUpload);
            
            // 工具栏按钮
            document.getElementById('fullscreen-btn').addEventListener('click', toggleFullscreen);
            document.getElementById('upload-btn').addEventListener('click', showUploadPanel);
            document.getElementById('reset-view-btn').addEventListener('click', resetView);
            
            // 模态框按钮
            document.getElementById('close-message').addEventListener('click', () => hideModal('message-modal'));
            
            // 双击全屏
            renderer.domElement.addEventListener('dblclick', toggleFullscreen);
        }
        
        // 处理文件选择
        function handleFileUpload(event) {
            const file = event.target.files[0];
            if (!file) return;
            
            if (!file.type.match('image.*')) {
                showMessage('错误', '请选择图片文件');
                return;
            }
            
            // 显示选择进度
            document.getElementById('progress-container').style.display = 'block';
            document.getElementById('progress').style.width = '0%';
            document.getElementById('progress-text').textContent = '选择进度: 0%';
            
            // 模拟选择进度
            let progress = 0;
            const progressInterval = setInterval(() => {
                progress += Math.random() * 10;
                if (progress >= 100) {
                    progress = 100;
                    clearInterval(progressInterval);
                }
                
                document.getElementById('progress').style.width = progress + '%';
                document.getElementById('progress-text').textContent = `选择进度: ${Math.round(progress)}%`;
            }, 200);
            
            document.getElementById('loading').style.display = 'flex';
            
            const reader = new FileReader();
            reader.onload = function(e) {
                clearInterval(progressInterval);
                document.getElementById('progress-container').style.display = 'none';
                
                const image = new Image();
                image.onload = function() {
                    // 检查图片比例是否为2:1
                    if (Math.abs(image.width / image.height - 2) > 0.1) {
                        showMessage('提示', '建议使用2:1比例的全景图(360°x180°)以获得最佳效果');
                    }
                    
                    const texture = new THREE.Texture(image);
                    texture.needsUpdate = true;
                    
                    createPanorama(texture);
                    
                    document.getElementById('loading').style.display = 'none';
                    document.getElementById('upload-container').classList.add('hidden');
                    showMessage('加载成功', '全景图已成功加载');
                };
                image.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }
        
        // 显示模态框
        function showModal(modalId) {
            document.getElementById(modalId).classList.add('active');
        }
        
        // 隐藏模态框
        function hideModal(modalId) {
            document.getElementById(modalId).classList.remove('active');
        }
        
        // 显示消息
        function showMessage(title, content) {
            document.getElementById('message-title').textContent = title;
            document.getElementById('message-content').textContent = content;
            showModal('message-modal');
        }
        
        // 窗口大小调整
        function onWindowResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }
        
        // 切换全屏
        function toggleFullscreen() {
            if (!document.fullscreenElement) {
                renderer.domElement.requestFullscreen().catch(err => {
                    showMessage('全屏错误', err.message);
                });
            } else {
                document.exitFullscreen();
            }
        }
        
        // 显示选择面板
        function showUploadPanel() {
            document.getElementById('upload-container').classList.remove('hidden');
        }
        
        // 重置视角
        function resetView() {
            if (controls) {
                controls.reset();
            }
        }
        
        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            
            if (controls) {
                controls.update();
            }
            
            renderer.render(scene, camera);
        }
        
        // 初始化应用
        window.onload = init;
    </script>
</body>
</html>